﻿using common.exceptions;
using common.helpers;
using common.img.helpers;
using webserver.core;

namespace img.mgr.Controllers
{
    public class ImgController : Controller
    {
        public HttpResponseActionModel View(ImgViewRequest request)
        {
            var img = default(string);
            try
            {
                var msg = "图片不存在或链接不正确";
                if (string.IsNullOrEmpty(request.FileName) ||
                    string.IsNullOrEmpty(request.Token))
                {
                    throw new WithCodeException((int)ErrorCodeEnum.InvalidArguments, msg + "!");
                }

                request.Token = request.Token.Replace(" ", "+");

                var key = SecurityHelper.DESDecrypt(request.Token, "1qaz2WSX", "1qaz2WSX");
                var parts = key.Split(',');
                var fullname = parts[1];
                if (request.FileName != Path.GetFileName(fullname))
                {
                    throw new WithCodeException((int)ErrorCodeEnum.InvalidArguments, msg + ".");
                }

                var dt = DateTime.Parse(parts[0]);
                var ts = DateTime.Now - dt;
                if (ts.TotalSeconds > 0)
                {
                    throw new WithCodeException((int)ErrorCodeEnum.InvalidArguments, msg + "。");
                }

                if (!System.IO.File.Exists(fullname))
                {
                    throw new WithCodeException((int)ErrorCodeEnum.InvalidArguments, msg);
                }

                img = fullname;
            }
            catch (Exception ex)
            {
                LogHelper.WriteAsync($"查看图片失败：{request.FileName}{Environment.NewLine}{ex}", "ViewImgError");
            }

            if (string.IsNullOrEmpty(img) || System.IO.File.Exists(img) == false)
            {
                img = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "img404.png");
            }

            var ext = Path.GetExtension(img);
            if (!string.IsNullOrEmpty(ext))
            {
                ext = ext.ToLower();
            }

            string contentType;
            switch (ext)
            {
                case ".jpeg":
                case ".jpg":
                    {
                        contentType = "image/jpeg";
                        break;
                    }
                case ".png":
                    {
                        contentType = "image/png";
                        break;
                    }
                default:
                    {
                        contentType = "application/octet-stream";
                        break;
                    }
            }

            var bytes = System.IO.File.ReadAllBytes(img);
            if (request.Size.GetValueOrDefault() < 100)
            {
                return File(bytes, contentType, request.FileName);
            }

            var thumbBytes = ImgHelper.Thumb(bytes, ext, request.Size.GetValueOrDefault());
            return File(thumbBytes, contentType, request.FileName);
        }
    }
}
